import java.util.Arrays;

public class Solution922 {
    public int[] sortArrayByParityII(int[] nums) {
        int l=0,r=1;
        while (l<nums.length){
            while (l<nums.length && (nums[l]+l)%2==0){
                l+=2;
            }
            while (r<nums.length && (nums[r]+r)%2==0){
                r+=2;
            }
            if (l<nums.length){
                int temp=nums[l];
                nums[l]=nums[r];
                nums[r]=temp;
                l+=2;
                r+=2;
            }
        }
        return nums;
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(new Solution922().sortArrayByParityII(new int[]{4,2,5,7})));
    }
}
